<?php
namespace App\Lib;
class sms{
    protected $appId = 1400110034;
    protected $appKey = 'aa036a7f45b8ac3edcdfc6917acd399d';
    protected $tpl_id = 343568;

    /**
     * 指定模板发送短信
     * @param  [type] $tel [短信接收人]
     * @return [type]      [description]
     */
    public function sms($tel){
        $appId = $this->appId;//模板标识
        $Appkey = $this->appKey;//短信对接秘钥
        $random = $this->generate_code();//得到随机数
        $tpl_id = $this->tpl_id;//腾讯云上的短信模板ID
        $time = time();//获取当前时间戳
        $sig = $this->sigsum($tel,$Appkey,$random,$time);//app凭证
        //建立数组数据包
        $params1 = array(
            "tel"=>array('nationcode'=>'86','mobile'=>$tel),
            "tpl_id"=>$tpl_id,
            "params"=>array($random,5),
            "sig"=>$sig,
            "time"=>$time
        );
        //将数组数据包转换成JSON数据包
        $params = json_encode($params1);

        //服务器请求路径
        $url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid={$appId}&random={$random}";
        //调用请求
        $data = $this->http_post($url,$params);
        $data = json_decode(json_encode(json_decode($data)),TRUE);
        //判断是否返回成功
        if($data['result']==0){
            //将随机数放进数组里
            $data['random'] = $random;
        }
        //返回数据
        return $data;
    }

    /**
     * php生成四位随机数
     * @param  integer $length [随机数长度]
     * @return [type]   data   [返回生成的随机数]
     */
    function generate_code($length = 6) {
        $min = pow(10 , ($length - 1));
        $max = pow(10, $length) - 1;
        return rand($min, $max);
    }

    /**
     * app凭证
     * @param  [type] $mobile  [电话]
     * @param  [type] $AppKey  [sdkappid对应的appkey，需要业务方高度保密]
     * @param  [type] $strRand [系统生成的随机数]
     * @param  [type] $strTime [当前时间戳]
     * @return [type]  data    [app凭证]
     */
    public function sigsum($tel,$appkey,$strRand,$strTime){
        //hash算法指定sha256算法
        $str = 'appkey='.$appkey.'&random='.$strRand.'&time='.$strTime.'&mobile='.$tel;
        $sig = hash("sha256",$str);
        return $sig;
    }

    /**
     * @param string $api_url 接口地址
     * @param array $param post参数
     * @param int $timeout 超时时间
     * @return bool
     */
    private function http_post( $api_url = '' , $param = array() , $timeout = 45 ){

        if( !$api_url ){
            die("error api_url");
        }

        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $api_url );

        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_0 );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt( $ch, CURLOPT_HEADER, FALSE);

        if( parse_url( $api_url )['scheme'] == 'https' ){
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , FALSE);
        }

        curl_setopt( $ch, CURLOPT_HTTPAUTH , CURLAUTH_BASIC);
//        curl_setopt( $ch, CURLOPT_USERPWD , 'api:key-'.$this->_api_key );
        curl_setopt( $ch, CURLOPT_POST, TRUE);
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $param );

        $res  = curl_exec( $ch );
        $error = curl_error( $ch );
        curl_close( $ch );
        if( $error ){
            $this->_last_error[] = $error;
            return FALSE;
        }
        return $res;
    }
}
